// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/json.h>

#include <utility>

#include <pollux/core/plan_node.h>
#include <pollux/exec/plan_node_stats.h>
#include <pollux/exec/task_trace_reader.h>
#include <pollux/exec/trace_util.h>
#include <pollux/testing/exec/util/assert_query_builder.h>
#include <pollux/plan/plan_builder.h>
#include <pollux/common/file/temp_directory_path.h>
#include <pollux/tool/trace/operator_replayer_base.h>

#include <pollux/tool/trace/trace_replay_task_runner.h>

using namespace kumo::pollux;
using namespace kumo::pollux::plan;

namespace kumo::pollux::tool::trace {
OperatorReplayerBase::OperatorReplayerBase(
    std::string traceDir,
    std::string queryId,
    std::string taskId,
    std::string nodeId,
    std::string operatorType,
    const std::string& driverIds,
    uint64_t queryCapacity,
    melon::Executor* executor)
    : queryId_(std::string(std::move(queryId))),
      taskId_(std::move(taskId)),
      nodeId_(std::move(nodeId)),
      operatorType_(std::move(operatorType)),
      taskTraceDir_(
          exec::trace::getTaskTraceDirectory(traceDir, queryId_, taskId_)),
      nodeTraceDir_(exec::trace::getNodeTraceDirectory(taskTraceDir_, nodeId_)),
      fs_(filesystems::getFileSystem(taskTraceDir_, nullptr)),
      pipelineIds_(exec::trace::listPipelineIds(nodeTraceDir_, fs_)),
      driverIds_(
          driverIds.empty() ? exec::trace::listDriverIds(
                                  nodeTraceDir_,
                                  pipelineIds_.front(),
                                  fs_)
                            : exec::trace::extractDriverIds(driverIds)),
      queryCapacity_(queryCapacity == 0 ? memory::kMaxMemory : queryCapacity),
      executor_(executor) {
  POLLUX_USER_CHECK(!taskTraceDir_.empty());
  POLLUX_USER_CHECK(!taskId_.empty());
  POLLUX_USER_CHECK(!nodeId_.empty());
  POLLUX_USER_CHECK(!operatorType_.empty());
  if (operatorType_ == "HashJoin") {
    POLLUX_USER_CHECK_EQ(pipelineIds_.size(), 2);
  } else {
    POLLUX_USER_CHECK_EQ(pipelineIds_.size(), 1);
  }
  POLLUX_CHECK_NOT_NULL(executor_);

  const auto taskMetaReader = exec::trace::TaskTraceMetadataReader(
      taskTraceDir_, memory::MemoryManager::getInstance()->tracePool());
  queryConfigs_ = taskMetaReader.queryConfigs();
  connectorConfigs_ = taskMetaReader.connectorProperties();
  planFragment_ = taskMetaReader.queryPlan();
  queryConfigs_[core::QueryConfig::kQueryTraceEnabled] = "false";
}

RowVectorPtr OperatorReplayerBase::run(bool copyResults) {
  auto queryCtx = createQueryCtx();
  std::shared_ptr<exec::test::TempDirectoryPath> spillDirectory;
  if (queryCtx->queryConfig().spillEnabled()) {
    spillDirectory = exec::test::TempDirectoryPath::create();
  }

  TraceReplayTaskRunner traceTaskRunner(createPlan(), std::move(queryCtx));
  auto [task, result] =
      traceTaskRunner.maxDrivers(driverIds_.size())
          .spillDirectory(spillDirectory ? spillDirectory->getPath() : "")
          .run(copyResults);
  printStats(task);
  return result;
}

core::PlanNodePtr OperatorReplayerBase::createPlan() {
  const auto* replayNode = core::PlanNode::findFirstNode(
      planFragment_.get(),
      [this](const core::PlanNode* node) { return node->id() == nodeId_; });

  if (replayNode->name() == "TableScan") {
    return PlanBuilder()
        .addNode(replayNodeFactory(replayNode))
        .capturePlanNodeId(replayPlanNodeId_)
        .planNode();
  }

  return PlanBuilder(planNodeIdGenerator_)
      .traceScan(
          nodeTraceDir_,
          pipelineIds_.front(),
          driverIds_,
          exec::trace::getDataType(planFragment_, nodeId_))
      .addNode(replayNodeFactory(replayNode))
      .capturePlanNodeId(replayPlanNodeId_)
      .planNode();
}

std::shared_ptr<core::QueryCtx> OperatorReplayerBase::createQueryCtx() {
  static std::atomic_uint64_t replayQueryId{0};
  auto queryPool = memory::memoryManager()->addRootPool(
      fmt::format("{}_replayer_{}", operatorType_, replayQueryId++),
      queryCapacity_);
  std::unordered_map<std::string, std::shared_ptr<config::ConfigBase>>
      connectorConfigs;
  for (auto& [connectorId, configs] : connectorConfigs_) {
    connectorConfigs.emplace(
        connectorId, std::make_shared<config::ConfigBase>(std::move(configs)));
  }
  return core::QueryCtx::create(
      executor_,
      core::QueryConfig{queryConfigs_},
      std::move(connectorConfigs),
      nullptr,
      std::move(queryPool),
      executor_);
}

std::function<core::PlanNodePtr(std::string, core::PlanNodePtr)>
OperatorReplayerBase::replayNodeFactory(const core::PlanNode* node) const {
  return [=](const core::PlanNodeId& nodeId,
             const core::PlanNodePtr& source) -> core::PlanNodePtr {
    return createPlanNode(node, nodeId, source);
  };
}

void OperatorReplayerBase::printStats(
    const std::shared_ptr<exec::Task>& task) const {
  const auto planStats = exec::toPlanStats(task->taskStats());
  const auto& stats = planStats.at(replayPlanNodeId_);
  for (const auto& [name, operatorStats] : stats.operatorStats) {
    KLOG(INFO) << "Stats of replaying operator " << name << " : "
              << operatorStats->toString();
  }
  KLOG(INFO) << "Memory usage: " << task->pool()->treeMemoryUsage(false);
}
} // namespace kumo::pollux::tool::trace
